package com.sc.nft.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.SysMemberDao;
import com.sc.nft.entity.SysMember;
import com.sc.nft.service.SysMemberService;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.sup.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 系统会员表服务接口实现
 *
 * @author zhangxiangyu
 * @since 2022-02-02 17:11:20
 * @description 
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysMemberServiceImpl implements SysMemberService {
    private final SysMemberDao sysMemberDao;

    @Override
    public SysMember login(String account, String password) throws GlobalException {
        String md5 = SecureUtil.md5(password);
        SysMember sysMember=sysMemberDao.login(account);
        if (sysMember == null ) {
            throw new GlobalException(ErrorCode.NO_LOGIN_ERROR);
        }
        if (!sysMember.getUserPassword().equals(md5)){
            throw new GlobalException(ErrorCode.PASSWORD_ERROR);
        }
        return sysMember;
    }

    public static void main(String[] args) {
        System.out.println(SecureUtil.md5("054394"));
    }

    @Override
    public boolean checkUserAccountExists(String account) {
        return ObjectUtil.isNotNull(sysMemberDao.getByAccount(account));
    }

    @Override
    public Page query(String account, Long roleId, String userName, Integer userSex, Date startBirthday, Date endBirthday, String phone, int pageNo, int pageSize) {
        return sysMemberDao.query(account, roleId, userName, userSex, startBirthday, endBirthday, phone, pageNo, pageSize);
    }

    @Override
    public SysMember selectByRoleId(Long id) {
        return sysMemberDao.selectByRoleId(id);
    }

    @Override
    public SysMember getById(Long id) {
        return sysMemberDao.getById(id);
    }
}